#ifndef TITLEBAR_H
#define TITLEBAR_H

#include <QPushButton>
#include <QLabel>
#include <QHBoxLayout>
#include <QFlags>
#include <QDebug>
#include <QMenu>
#include "frameless/frameless_helper.h"
// titleBar宽度
static const int TitleBarWidth = 3;

/*---------------------------------*/
//标题栏按钮类
class TitleButton : public QPushButton
{
    Q_OBJECT
public:
    explicit TitleButton(QWidget *parent = 0);
    ~TitleButton();
public:
    void loadPixmap(const QString& picName);
    void setPixmap(const QPixmap& _pixmap);

    void loadBackground(const QString& picName);
    void setBackground(const QPixmap& _pixmap);

protected:
    void enterEvent(QEvent *) Q_DECL_OVERRIDE;
    void leaveEvent(QEvent *) Q_DECL_OVERRIDE;
    void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
    void mouseReleaseEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
    void paintEvent(QPaintEvent * ) Q_DECL_OVERRIDE;
private:
    void paintPixmap();


private:
    //按钮状态
    enum ButtonStatus {
        NORMAL,
        HOVER,
        PRESSED,
        DISABLED,
        CHECKED
    };
    //按钮图片类型
    enum PixmapType {
        FOREGROUND,
        BACKGROUND
    };


private:
    QPixmap      pixmap;          // 按钮的图片，
    PixmapType   pixmapType   = FOREGROUND;      // 图片类型
    ButtonStatus buttonStates = NORMAL;    // 当前状态
    int          width        = 32;      // 按钮宽度
    int          height       = 32;     // 按钮高度
    bool         pressed      = false; // 是否被按下
};

/*---------------------------------*/
// 标题栏元素
enum TitleBarElementType {
    ICON    = 0x001,
    TITLE   = 0x002,
    MENU    = 0x004,
    SKIN    = 0x008,    // 更换皮肤按钮
    MIN     = 0x010,
    MAX     = 0x020,
    RESTORE = 0x040,    // 界面最大化之后恢复按钮
    CLOSE   = 0x080
};
Q_DECLARE_FLAGS(TitleBarElementTypes, TitleBarElementType)
Q_DECLARE_OPERATORS_FOR_FLAGS(TitleBarElementTypes)


// 主窗口标题栏
class MainWindowTitleBar : public QWidget
{
    Q_OBJECT
public:
    MainWindowTitleBar(TitleBarElementTypes types, QWidget *parent = nullptr);
    ~MainWindowTitleBar();
public:
    void setElement(TitleBarElementType type, const QString content);
    bool isMaximized() {
        return maxed;
    }
signals:
    void changeSkin(QString skinFile); // 换皮肤信号
private slots:
    void showMaxState();

protected:
    void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
    void mouseReleaseEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
    void mouseMoveEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
    void mouseDoubleClickEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
    //继承QWidget，则需要重写paintEvent函数，否则无法使用QSS
    void paintEvent(QPaintEvent* e) Q_DECL_OVERRIDE;
private:
    QHBoxLayout *titleLayout = nullptr;

    QLabel      *icon        = nullptr;
    QLabel      *title       = nullptr;

    TitleButton *menuButton  = nullptr;
    TitleButton *skinButton  = nullptr;
    TitleButton *minButton   = nullptr;
    TitleButton *maxButton   = nullptr;
    TitleButton *closeButton = nullptr;
    QPixmap     restorePixmap;
    QPixmap     maxPixmap;

    QRect       normalRect   = QRect(0,0,0,0);
    QPoint      clickPos     = QPoint(0,0);

    bool        pressed      = false;
    bool        maxed        = false;

};



// 子窗口标题栏
class widgetTitleBar : public QWidget
{
    Q_OBJECT
public:
    widgetTitleBar(QWidget *parent = nullptr);
    ~widgetTitleBar();

public:
    void setTitle(const QString &_title);
signals:
    void closeParent(); //父窗口关闭信号
protected:
    //继承QWidget，则需要重写paintEvent函数，否则无法使用QSS
    void paintEvent(QPaintEvent* e) Q_DECL_OVERRIDE;
private:
    QHBoxLayout *titleLayout = nullptr;
    QLabel      *title       = nullptr;
    TitleButton *closeButton = nullptr;
    QWidget *parentSubWindow = nullptr;

};




#endif // TITLEBAR_H
